Passed
Push — master ( 8e49de...855750 )
by Joe Nilson
03:05
created

EditFacturaCliente.js ➔ cargarInfoCliente   A

Complexity

Conditions 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
dl 0
loc 17
rs 9.8
c 0
b 0
f 0
1
2
/*
3
 * This file is part of FacturaScripts - Dominican Republic Plugin
4
 * Copyright (C) 2013-2019 Carlos Garcia Gomez <[email protected]>
5
 * Copyright (C) 2019-2020 Joe Nilson <[email protected]>
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20 View Code Duplication
function businessDocViewSubjectChanged()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
21
{
22
    var data = {};
23
    $.each($("#" + businessDocViewFormName).serializeArray(), function (key, value) {
0 ignored issues
show
Bug introduced by
The variable businessDocViewFormName seems to be never declared. If this is a global, consider adding a /** global: businessDocViewFormName */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
24
        data[value.name] = value.value;
25
    });
26
    data.action = "subject-changed";
27
    logConsole(data,"subject-changed");
28
29
    $.ajax({
30
        type: "POST",
31
        url: businessDocViewUrl,
0 ignored issues
show
Bug introduced by
The variable businessDocViewUrl seems to be never declared. If this is a global, consider adding a /** global: businessDocViewUrl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
32
        dataType: "json",
33
        data: data,
34
        success: function (results) {
35
            $("#doc_codpago").val(results.codpago);
36
            $("#doc_codserie").val(results.codserie);
37
            $("#formEditFacturaCliente select[name=ncftipopago]").val(results.ncftipopago);
38
39
            /**
40
             * Review the doc_codsubtipodoc existence,
41
             * if it exist we put the value from the customer data
42
             */
43
44
            if ($("#doc_codsubtipodoc").length !== 0) {
45
                $("#doc_codsubtipodoc").val(results.codsubtipodoc);
46
            }
47
            logConsole(results.codsubtipodoc,"codsubtipodoc");
48
            /**
49
             * Review the doc_codopersaciondoc existence,
50
             * if it exist we put the value from the customer data
51
             */
52
            if ($("#doc_codoperaciondoc").length !== 0) {
53
                $("#doc_codoperaciondoc").val(results.codoperaciondoc);
54
            }
55
            logConsole(results.codoperaciondoc,"codoperaciondoc");
56
            
57
            logConsole(results,"results");
58
59
            businessDocViewRecalculate();
60
        },
61
        error: function (xhr, status, error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter status is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
62
            alert(xhr.responseText);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
63
        }
64
    });
65
}
66
67
async function cargarInfoCliente()
68
{
69
    return $.ajax({
70
        url: 'ListNCFTipo',
71
        async: true,
72
        data: {'action': 'busca_infocliente', 'codcliente': $("#codclienteAutocomplete").val()},
73
        type: 'POST',
74
        datatype: 'json',
75
        success: function (response) {
76
            let data = JSON.parse(response);
77
            return data;
78
        },
79
        error: function (xhr, status) {
80
            alert('Ha ocurrido algún tipo de error ' + status);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
81
        }
82
    });
83
}
84
85
async function cargarTipoPago()
86
{
87
    return $.ajax({
88
        url: 'ListNCFTipoPago',
89
        async: true,
90
        data: {'action': 'busca_pago', 'tipopago': '01'},
91
        type: 'POST',
92
        datatype: 'json',
93
        success: function (response) {
94
            let data = JSON.parse(response);
95
            return data;
96
        },
97
        error: function (xhr, status) {
98
            alert('Ha ocurrido algún tipo de error ' + status);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
99
        }
100
    });
101
}
102
103 View Code Duplication
async function cargarTipoMovimiento()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
104
{
105
    return $.ajax({
106
        url: 'ListNCFTipoMovimiento',
107
        async: true,
108
        data: {'action': 'busca_movimiento', 'tipomovimiento': 'VEN'},
109
        type: 'POST',
110
        datatype: 'json',
111
        success: function (response) {
112
            let data = JSON.parse(response);
113
            return data;
114
        },
115
        error: function (xhr, status) {
116
            alert('Ha ocurrido algún tipo de error ' + status);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
117
        }
118
    });
119
}
120
121
async function businessDocViewSave()
122
{
123
    $("#btn-document-save").prop("disabled", true);
124
    var infoCliente = await cargarInfoCliente();
125
    logConsole(infoCliente, 'infoCliente');
126
    var datosCliente = JSON.parse(infoCliente);
127
    var tipoPago = await cargarTipoPago();
128
    var datosPago = JSON.parse(tipoPago);
129
    var tipoNCFs = await cargarTipoNCF('Ventas');
130
    //logConsole(tipoNCFs, 'tipoNCFs');
131
    var datosTipoNCFs = JSON.parse(tipoNCFs);
132
    let selectTiposNCF = "";
133
    var descInfoClienteTipoComprobante = '';
134
    $.each(datosTipoNCFs.tipocomprobantes, function (i, value) {
135
        let defaultSelected = (datosCliente.infocliente.tipocomprobante === value.tipocomprobante) ? 'selected' : '';
136
        descInfoClienteTipoComprobante = (datosCliente.infocliente.tipocomprobante === value.tipocomprobante)
137
            ? value.descripcion : descInfoClienteTipoComprobante;
138
        selectTiposNCF += '<option value="'+value.tipocomprobante+'"'+defaultSelected+'>'+value.descripcion+'</option>';
139
    });
140
141
    var ncfTipoPagoCliente = datosCliente.infocliente.ncftipopago;
142
    var readOnlySelects = ($("#formSalesDocumentLine #doc_idestado").val() === '11');
143
    var descInfoClienteTipoPago = '';
144
    let selectOptionsPagos = "";
145
    logConsole(ncfTipoPagoCliente, 'ncfTipoPagoCliente');
146
    $.each(datosPago.pagos, function (i, value) {
147
        let defaultSelected = ((value.codigo === '17' && ncfTipoPagoCliente === '') || ncfTipoPagoCliente === value.codigo) ? 'selected' : '';
148
        descInfoClienteTipoPago = (datosCliente.infocliente.ncftipopago === value.codigo)
149
            ? value.descripcion : descInfoClienteTipoPago;
150
        let noSelected = ($("#formSalesDocumentLine #doc_idestado").val() === '11' && defaultSelected !== 'selected') ? ' disabled' : '';
151
        selectOptionsPagos += '<option value="'+value.codigo+'"'+defaultSelected+noSelected+'>'+value.descripcion+'</option>';
152
    });
153
154
    var tipoMovimiento = await cargarTipoMovimiento();
155
    var datosMovimiento = JSON.parse(tipoMovimiento);
156
157
    let selectOptionsMovimientos = "";
158 View Code Duplication
    $.each(datosMovimiento.movimientos, function (i, value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
159
        let defaultSelected = (value.codigo === '1') ? 'selected' : '';
160
        let noSelected = ($("#formSalesDocumentLine #doc_idestado").val() === '11' && defaultSelected !== 'selected') ? ' disabled' : '';
161
        selectOptionsMovimientos += '<option value="'+value.codigo+'"'+defaultSelected+noSelected+'>'+value.descripcion+'</option>';
162
    });
163
164
    let message = setBusinessDocViewModalSave(
165
        'Cliente',
166
        readOnlySelects,
167
        descInfoClienteTipoComprobante,
168
        descInfoClienteTipoPago,
169
        selectTiposNCF,
170
        selectOptionsPagos,
171
        selectOptionsMovimientos
172
    );
173
174
    executeModal(
175
        'completeNCFData',
176
        'Complete la información faltante',
177
        message,
178
        'default',
179
        'saveBussinessDocument'
180
    );
181
182
    $("#btn-document-save").prop("disabled", false);
183
}
184